package example.helloworld;

import grpc.htsoft.helloworld.*;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.IOException;

public class server {


        static public void main(String [] args) throws IOException, InterruptedException {
        Server server = ServerBuilder.forPort(50054)
                .addService(new GreetingServiceImpl()).build();

        System.out.println("Starting server...");
        server.start();
        System.out.println("Server started!");
        server.awaitTermination();
    }

        public static class GreetingServiceImpl extends GreeterGrpc.GreeterImplBase {
            @Override
            public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
                System.out.println(request);

                String greeting = "Hello sayHello java server, " + request.getName();

                HelloReply response = HelloReply.newBuilder().setMessage(greeting).build();

                responseObserver.onNext(response);
                responseObserver.onCompleted();
            }
            public void getUser(User request, StreamObserver<UserReply> responseObserver) {
                System.out.println(request);
                System.out.println(request.getClass());

                String greeting = "Hello getuser  java server, " + request.getName();

                UserReply response = UserReply.newBuilder().
                        setName(greeting)
                        .setAge(44)
                        .setId("001")

                        .build();

                responseObserver.onNext(response);
                responseObserver.onCompleted();
            }
            public void getUser2(User request, StreamObserver<User> responseObserver) {
                System.out.println(request);
                System.out.println(request.getClass());

                String greeting = "Hello getuser2  java server, " + request.getName();

                User response = User.newBuilder().
                        setName(greeting)
                        .setAge(44)
                        .setId("001")

                        .build();

                responseObserver.onNext(response);
                responseObserver.onCompleted();
            }
            public void getDataSet(User request, StreamObserver<DataSet> responseObserver) {
                System.out.println(request);
                System.out.println(request.getClass());

                Cell c = Cell.newBuilder().setKey("key").setValue("value").build();


                Cell c2 = Cell.newBuilder().setKey("key2").setValue("value2").build();


                Cell c3 = Cell.newBuilder().setKey("key3").setValue("value3").build();


                Row.Builder r2 = Row.newBuilder();
                r2.addCells(c3);
                r2.addCells(c2);
                r2.addCells(c);


                Row.Builder r3 = Row.newBuilder();
                Cell c4 = Cell.newBuilder().setKey("key4").setValue("value4").build();
                r3.addCells(c4);
                DataSet.Builder ds = DataSet.newBuilder();



                ds.addRows(r2);
                ds.addRows(r3);


//                ds.build();
//                DataSet d = DataSet.newBuilder().addRows(r2).build();
                responseObserver.onNext(ds.build());
                responseObserver.onCompleted();

            }



        }

}
